package com.bonshotels.DataLayer.CtrlDadesHibernate;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;

import com.bonshotels.DomainLayer.CtrlDades.ICtrlHotel;
import com.bonshotels.DomainLayer.DomainModel.Hotel;
import com.bonshotels.Excepcions.HotelNoExisteix;

/**
 * Aquesta classe es la implementacio concreta per a Hibernate de ICtrlHotel, es un producte
 * concret dins del patro factoria abstracta.
 * @autor Marc Mauri Alloza
 */
class CtrlHotelHibernate extends CtrlGeneralHibernate implements ICtrlHotel {

	/* (non-Javadoc)
	 * @see com.bonshotels.DomainLayer.CtrlDades.ICtrlHotel#obte(java.lang.String)
	 */
	@Override
	public Hotel obte(String hotel) throws HotelNoExisteix {
		Session session = getSession();
		Hotel h = (Hotel) session.get(Hotel.class, hotel);
		if (h == null) throw new HotelNoExisteix();
		return h;
	}

	/* (non-Javadoc)
	 * @see com.bonshotels.DomainLayer.CtrlDades.ICtrlHotel#existeix(java.lang.String)
	 */
	@Override
	public boolean existeix(String hotel) {
		Query query = getSession().createQuery("select * from Hotel where nom = :nom");
		query.setParameter("nom", hotel);
		return query.list().size() == 1;
	}

	/* (non-Javadoc)
	 * @see com.bonshotels.DomainLayer.CtrlDades.ICtrlHotel#obteTots()
	 */
	@Override
	public Set<Hotel> obteTots() {
		Collection<Hotel> hots = getSession().createCriteria(Hotel.class).list();
		return new HashSet<Hotel>(hots);
	}

}
